package com.example.hello.controller;

import com.example.hello.common.PageResult;
import com.example.hello.common.Result;
import com.example.hello.entity.Emp;
import com.example.hello.entity.EmpExpr;
import com.example.hello.service.EmpService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;
    
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 1. 员工分页查询
     */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "") String name,
                      @RequestParam(required = false) Integer gender,
                      @RequestParam(required = false) String hireDateStart,
                      @RequestParam(required = false) String hireDateEnd,
                      @RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            LocalDate startDate = hireDateStart != null ? LocalDate.parse(hireDateStart) : null;
            LocalDate endDate = hireDateEnd != null ? LocalDate.parse(hireDateEnd) : null;
            
            PageResult pageResult = empService.page(name, gender, startDate, endDate, page, pageSize);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 2. 新增员工（支持JSON格式）
     */
    @PostMapping
    public Result add(@RequestBody Emp emp) {
        try {
            empService.add(emp, null);
            return Result.success("添加成功", null);
        } catch (Exception e) {
            return Result.error("添加失败：" + e.getMessage());
        }
    }

    /**
     * 3. 根据ID查询员工详情
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        try {
            Emp emp = empService.getById(id);
            if (emp == null) {
                return Result.error("员工不存在");
            }
            return Result.success(emp);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 4. 修改员工（支持JSON格式）
     */
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        try {
            empService.update(emp, null);
            return Result.success("修改成功", null);
        } catch (Exception e) {
            return Result.error("修改失败：" + e.getMessage());
        }
    }

    /**
     * 5. 删除员工
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        try {
            empService.deleteById(id);
            return Result.success("删除成功", null);
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 6. 批量删除员工
     */
    @DeleteMapping
    public Result deleteBatch(@RequestParam("ids") String ids) {
        try {
            List<Integer> idList = Arrays.stream(ids.split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            for (Integer id : idList) {
                empService.deleteById(id);
            }
            return Result.success("批量删除成功", null);
        } catch (Exception e) {
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }
} 